package com.nutricia.core.service.impl;

import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.nutricia.common.dal.persistant.MenuAndPermission;
import com.nutricia.common.dal.persistant.SystemPermission;
import com.nutricia.common.util.CheckUtil;
import com.nutricia.core.ex.BizErrorCode;
import com.nutricia.core.ex.BizException;
import com.nutricia.core.model.domain.MenuAndPermissionDO;
import com.nutricia.core.model.domain.SystemPermissionDO;
import com.nutricia.core.service.SystemPermissionService;
import com.nutricia.core.service.convertor.MenuUnionPermissionServiceConvertor;
import com.nutricia.core.service.convertor.SystemPermissionServiceConvertor;

/**
 * 系统权限serviceImpl
 *
 * @author: huangsheng
 * @date: 2017/10/24
 */
@Service("systemPermissionService")
public class SystemPermissionServiceImpl extends AbstractServiceImpl implements SystemPermissionService {

    /**
     * 分页获取菜单权限数据
     *
     * @author: huangsheng
     * @date: 2017/10/24 下午2:47
     */
    @Override
    public List<SystemPermissionDO> getSystemPermission(String menuId) {
        try {
            List<SystemPermission> systemPermissions = systemPermissionMapper.queryPermissionByMenuId(menuId);
            return SystemPermissionServiceConvertor.convertToSystemPermissionDOs(systemPermissions);
        } catch (Exception e) {
            logger.error("分页获取菜单权限数据：{}", e);
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "系统异常");
        }
    }

    /**
     * 获取所有菜单的权限数据
     *
     * @author: huangsheng
     * @date: 2017/10/24 下午9:08
     */
    @Override
    public List<MenuAndPermissionDO> getMenuUnionPermissions() {
        try {
            List<MenuAndPermission> menuAndPermissions = menuAndPermissionMapper.queryAllMenuAndPermission();
            logger.info("获取一级菜单数据的数量：{}", menuAndPermissions.size());
            return MenuUnionPermissionServiceConvertor.convertToMenuAndPermissionDoS(menuAndPermissions);
        } catch (Exception e) {
            logger.error("获取所有菜单的权限数据异常：{}", e);
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "系统异常");
        }
    }

    /**
     * 查询角色拥有的权限ID集合
     *
     * @author: huangsheng
     * @date: 2017/10/24 下午9:38
     */
    @Override
    public List<String> getPermissionByRoleId(String roleId) {

        return rolePermissionMapper.queryPermissionByRoleId(roleId);
    }

    /**
     * 新增权限
     *
     * @author: huangsheng
     * @date: 2017/10/26 下午2:52
     */
    @Override
    public String createSystemPermission(SystemPermissionDO systemPermissionDO) {

        int permissionCodeCount = systemPermissionMapper.checkPermissionCode(systemPermissionDO.getPermissionCode());
        logger.info("查询到 permissionCode = {}， 的数量在库里是 {}", systemPermissionDO.getPermissionCode(), permissionCodeCount);
        CheckUtil.isTrue(permissionCodeCount == 0, BizErrorCode.PERMISSION_CODE_IS_EXIST, BizErrorCode.PERMISSION_CODE_IS_EXIST.getDescription());

        try {
            SystemPermission systemPermission = new SystemPermission();
            String uuid = UUID.randomUUID().toString();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
            logger.info("准备创建权限，ID = {}，菜单ID = {}", uuid, systemPermissionDO.getMenuId());
            systemPermission.setId(uuid);
            systemPermission.setMenuId(systemPermissionDO.getMenuId());
            systemPermission.setPermissionCode(systemPermissionDO.getPermissionCode());
            systemPermission.setPermissionName(systemPermissionDO.getPermissionName());

            int successInsertCount = systemPermissionMapper.insertSystemPermission(systemPermission);
            logger.info("成功插入 {} 条数据", successInsertCount);
            return uuid;
        } catch (Exception e) {
            logger.error("创建权限时异常：{}", e);
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "系统异常");
        }
    }

    /**
     * 更新权限数据
     *
     * @author: huangsheng
     * @date: 2017/10/26 下午3:02
     */
    @Override
    public void updateSystemPermission(SystemPermissionDO systemPermissionDO) {

        SystemPermission systemPermission = systemPermissionMapper.queryPermissionById(systemPermissionDO.getId());
        CheckUtil.isTrue(systemPermission != null,BizErrorCode.PERMISSION_IS_NOE_EXIST, BizErrorCode.PERMISSION_IS_NOE_EXIST.getDescription());
        logger.info("查询到权限数据： permissionCode = {}", systemPermission.getPermissionCode());
        try {
            if (!StringUtils.equals(systemPermission.getPermissionCode(), systemPermissionDO.getPermissionCode())) {
                int permissionCodeCount = systemPermissionMapper.checkPermissionCode(systemPermissionDO.getPermissionCode());
                logger.info("查询到 permissionCode = {}， 的数量在库里是 {}", systemPermissionDO.getPermissionCode(), permissionCodeCount);
                CheckUtil.isTrue(permissionCodeCount == 0, BizErrorCode.PERMISSION_CODE_IS_EXIST, BizErrorCode.PERMISSION_CODE_IS_EXIST.getDescription());
            }

            logger.info("准备修改权限，ID = {}", systemPermissionDO.getId());
            SystemPermission updateSystemPermission = new SystemPermission();
            updateSystemPermission.setId(systemPermissionDO.getId());
            updateSystemPermission.setMenuId(systemPermissionDO.getMenuId());
            updateSystemPermission.setPermissionCode(systemPermissionDO.getPermissionCode());
            updateSystemPermission.setPermissionName(systemPermissionDO.getPermissionName());

            int successUpdateCount = systemPermissionMapper.updateSystemPermission(updateSystemPermission);
            logger.info("成功执行 {} 条数据", successUpdateCount);
        } catch (Exception e) {
            logger.error("修改异常时异常：{}", e);
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "系统异常");
        }
    }

    /**
     * 删除权限
     *
     * @param permissionId 权限ID
     */
    @Override
    public void deleteSystemPermission(String permissionId) {
        try {
            logger.info("逻辑删除 ID = {} 的权限数据", permissionId);
            int successDeleteCount = systemPermissionMapper.deleteSystemPermission(permissionId);
            logger.info("成功执行 {} 条数据", successDeleteCount);
        } catch (Exception e) {
            logger.error("删除权限时异常：{}", e);
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "系统异常");
        }
    }

    /**
     * 获取所有权限code
     *
     * @return
     */
    @Override
    public List<String> getAllSystemPermission() {

        return systemPermissionMapper.queryAllPermissionCodes();
    }
}
